package com.chase.framerwork.action.excel;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;

import com.chase.framerwork.entity.User;
import com.chase.framerwork.excel.AbstractParseExcel;
import com.chase.framerwork.excel.Excel;

public class ParseUser extends AbstractParseExcel<User>
{	
	@Override
	public List<User> readExcel(HttpServletRequest request, Excel excel, Object... objects) throws Exception
	{

		List<User> userList = new ArrayList<User>();

		for (int i = 1; i <= excel.getRowCount(); i++)
		{
			String username = excel.getCellValue(i, 0);
			String password = excel.getCellValue(i, 1);
			String status = excel.getCellValue(i, 2);

			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setStatus(Integer.parseInt(status));

			userList.add(user);
		}

		return userList;
	}

	@Override
	public void writeExcel(HttpServletResponse response, Excel excel, List<User> resultList, Object... objects)
	        throws Exception
	{
		for (int i = 0; i < resultList.size(); i++)
		{
			User user = resultList.get(i);
			HSSFRow row = excel.createRow(i + 1); // 创建行

			row.createCell(0).setCellValue(user.getUsername()); // 创建列并且设置值
			row.createCell(1).setCellValue(user.getStatus());
			row.createCell(2).setCellValue(user.getCreateDateStr());
		}
	}
}
